<template>
  <div class="user-edit-container">
    <!-- Header 区域 -->
    <van-nav-bar
      title="编辑资料"
      left-arrow
      @click-left="$router.back()"
      fixed
    />
    <!-- 用户资料 -->
    <van-cell-group class="action-card">
      <van-cell title="头像" is-link center>
        <template #default>
          <van-image
            round
            class="avatar"
            :src="$store.state.userPhoto"
            @click="imageClickFn"
          />
          <input
            type="file"
            v-show="false"
            ref="iptFile"
            @change="onFileChange"
          />
        </template>
      </van-cell>
      <van-cell
        title="名称"
        is-link
        :value="profileObj.name"
        @click="nameClickfn"
      />
      <van-cell
        title="生日"
        is-link
        :value="profileObj.birthday"
        @click="birthdayClickFn"
      />
    </van-cell-group>
    <!-- 名称修改弹出框 -->
    <van-dialog
      v-model="show"
      title="修改名称"
      show-cancel-button
      :before-close="beforeCloseFn"
    >
      <input type="text" v-fofo v-model="inputUserName" />
    </van-dialog>
    <!-- 生日选择器 -->
    <van-popup v-model="dataTimePickerShow" round position="bottom" :style="{height:'40%'}">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @confirm="dateconfirmFn"
        @cancel='datecancelFn'
      />
    </van-popup>
  </div>
</template>

<script>
import { mapMutations } from 'vuex'
import { formatDate } from '@/utils/date'
import { Notify } from 'vant'
import { userProfileAPI, updateUserPhotoAPI, updateUserProfileAPI } from '@/api'
export default {
  name: 'UserEdit',
  data () {
    return {
      profileObj: {}, // 用户信息
      show: false, // 控制名称修改对话框显示和隐藏
      inputUserName: '', // 修改名字弹出框显示的名字
      minDate: new Date(1950, 1, 1), // 最小范围
      maxDate: new Date(), // 最大范围【系统当前时间】
      currentDate: '', // 显示时间【用户生日】
      dataTimePickerShow: false// 控制生日选择对话框的显示和隐藏
    }
  },
  async created () {
    const res = await userProfileAPI()
    this.profileObj = res.data.data
    // console.log(this.profileObj)
  },
  methods: {
    ...mapMutations(['SET_USERPHOTO']),
    // 图片-点击事件
    imageClickFn () { this.$refs.iptFile.click() },
    // 监听图片改变事件
    async onFileChange (e) {
      if (e.target.files.length === 0) return
      const formObj = new FormData()
      formObj.append('photo', e.target.files[0])// 请求体里面加入一对参数名和值
      const res = await updateUserPhotoAPI(formObj)
      console.log(res.data.data.photo)
      this.profileObj.photo = res.data.data.photo
      this.SET_USERPHOTO(res.data.data.photo)
    },
    // 名称-点击事件
    nameClickfn () {
      this.show = true
      this.inputUserName = this.profileObj.name
    },
    // 姓名-确认框-关闭前的回调函数
    beforeCloseFn (action, done) {
      if (action === 'confirm') { // 点击确认按钮
        const reg = /^[a-zA-Z0-9\u4e00-\u9fa5]{1,7}$/
        if (reg.test(this.inputUserName) === true) { // 通过校验
          updateUserProfileAPI({ name: this.inputUserName }) // 昵称
          this.profileObj.name = this.inputUserName
          done()
        } else {
          Notify({ type: 'warning', message: '请输入1-7位中英文数字组合' })
          done(false)
        }
      } else { // 点击取消按钮
        done()
      }
    },
    // 生日-点击事件
    birthdayClickFn () {
      this.dataTimePickerShow = true
      this.currentDate = new Date(this.profileObj.birthday)
      // confirm
    },
    // 日期选这期-取消
    datecancelFn () {
      this.dataTimePickerShow = false
    },
    // 日期选择器-确认
    async dateconfirmFn (value) {
      // console.log(value)
      await updateUserProfileAPI({ birthday: formatDate(value) })
      this.profileObj.birthday = formatDate(value)
      this.dataTimePickerShow = false
    }

  }
}
</script>

<style lang="less" scoped>
.user-edit-container {
  padding-top: 46px;
  .avatar {
    width: 50px;
    height: 50px;
  }
}
::v-deep .van-dialog__content {
  text-align: center;
  input {
    margin-top: 10px;
    padding: 0;
    border: none;
    outline: none;
    text-align: center;
  }
}
</style>
